package com.fancy.application.sys.auth.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fancy.application.common.controller.BaseController;
import com.fancy.application.common.utils.JwtUtil;
import com.fancy.application.common.vo.Result;
import com.fancy.application.sys.auth.vo.LoginForm;
import com.fancy.application.sys.auth.vo.Token;
import com.fancy.application.sys.org.entity.SysOrgPerson;
import com.fancy.application.sys.org.service.ISysOrgPersonService;
import com.fancy.application.sys.permission.service.ISysPermissionMatcher;
import com.fancy.application.sys.permission.service.ISysPermissionRoleService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author wison
 * @date 2019/8/26
 */
@Log4j2
@RestController
@RequestMapping("/auth/login")
public class LoginController extends BaseController {

    @Resource
    private ISysOrgPersonService sysOrgPersonService;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private ISysPermissionMatcher sysPermissionMatcher;
    @Resource
    private ISysPermissionRoleService sysPermissionRoleService;

    @PostMapping
    public Result login(@RequestBody LoginForm loginForm){
        SysOrgPerson person = sysOrgPersonService.getPersonByLoginInfo(loginForm);
        if(person!= null){
            String token = jwtUtil.sign(loginForm.getUserName(),person.getFdPassword());
            Token tokenModel = new Token(token,person.getFdLoginName());
            List<String> resList = sysPermissionRoleService.getResByUser(person);
            tokenModel.setPermissions(resList);
            return Result.ok().addData(tokenModel);
        }else{
            return Result.error("用户名或密码错误!");

        }

    }

    /**
     * 校验是否启动成功
     */
    @GetMapping("/serverStart")
    public Result serverStart(){
        return Result.ok().addData("fancyAdmin应用启动成功!");
    }

    /**
     * 校验是否有这个权限
     * @param permissionPath
     * @return
     */
    @GetMapping("/checkPermission")
    public Result checkPermission(@RequestParam("permissionPath") String permissionPath){
        log.debug("permissionPath:{}",permissionPath);
        Boolean flag = sysPermissionMatcher.doMatch(StrUtil.replace(permissionPath,"/form",""),"GET");
        return Result.ok().addData(flag);

    }
    @GetMapping(value = "/refreshToken")
    public Result refreshToken(HttpServletRequest request){
        String token = request.getParameter("token");
        if(StrUtil.isNotEmpty(token)){
            String username = jwtUtil.getUsername(token);
            QueryWrapper<SysOrgPerson> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fd_login_name",username).eq("fd_is_avaliable",true);
            SysOrgPerson person = sysOrgPersonService.getOne(queryWrapper);
            token = jwtUtil.sign(username,person.getFdPassword());
            return Result.ok().addData(token);
        }else{
            return Result.error("token为空,请重新登录");
        }

    }
}
